package com.itheima.mall.api.pms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.pms.ProductMapper;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ProductServiceApiImpl implements ProductServiceApi{
    @Autowired
    private ProductMapper productMapper;

    //创建商品
    @Override
    public void create(Product product) {
        productMapper.insert(product);
    }

    /**
     * 分页模糊查询商品
     * @param brandId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param productCategoryId
     * @param productSn
     * @param publishStatus
     * @param verifyStatus
     * @return
     */
    @Override
    public PageResult list(Integer brandId, String keyword, Integer pageNum, Integer pageSize,
                           Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        Page pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(brandId !=null,Product::getBrandId,brandId);
        queryWrapper.like(StrUtil.isNotEmpty(keyword),Product::getBrandName,keyword);
        queryWrapper.eq(productCategoryId != null,Product::getProductCategoryId,productCategoryId);
        queryWrapper.like(StrUtil.isNotEmpty(productSn),Product::getProductSn,productSn);
        queryWrapper.eq(publishStatus != null,Product::getPublishStatus,publishStatus);
        queryWrapper.eq(verifyStatus != null,Product::getVerifyStatus,verifyStatus);
        queryWrapper.orderByDesc(Product::getSort);
        Page page1 = productMapper.selectPage(pageInfo, queryWrapper);
        PageResult pageResult = new PageResult<>();
        pageResult.setList(page1.getRecords());
        pageResult.setTotalPage(Convert.toInt(page1.getPages()));
        pageResult.setPageNum(Convert.toInt(page1.getCurrent()));
        pageResult.setTotal(Convert.toInt(page1.getTotal()));
        return pageResult;
    }

    @Override
    public Product findProductById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updateDeleteStatus(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void updateById(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public Product updateInfoById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> simpleList(String keyword) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),Product::getKeywords,keyword);
        List<Product> productList = productMapper.selectList(queryWrapper);
        return productList;
    }
}
